//
//  RJWarmView.m
//  AirQuality
//
//  Created by Po on 15/4/7.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJWarmView.h"

@interface RJWarmView ()

@property (retain, nonatomic) NSDictionary * data;
@property (assign, nonatomic) CGFloat textHeight;

@end

@implementation RJWarmView

- (instancetype)initWithFrame:(CGRect)frame text:(NSDictionary *)text
{
    self = [super initWithFrame:frame];
    if (self) {
        _textHeight = 0;
        _data = [NSDictionary dictionaryWithDictionary:text];
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    CGFloat height = self.frame.size.height;
//    UILabel * tittleLabel = [self createLabelWithFrame:CGRectMake(0, 0, 80, height) text:@"温馨提示" color:[UIColor orangeColor] font:[UIFont boldSystemFontOfSize:15]];
//    [tittleLabel setTextAlignment:NSTextAlignmentRight];
//    [self addSubview:tittleLabel];
    UITextView * detailLabel = [self createTextWithFrame:CGRectMake(10, 0, ScreenWidth-20, height) text:[self getTextWithAQIValue:[_data[@"aqi"] integerValue]] color:GetRGBColor(85, 85, 85, 1) font:[UIFont systemFontOfSize:14]];
//    CGRect rect = CGRectMake(0, 0, ScreenWidth, detailLabel.frame.size.height);
    [detailLabel setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:detailLabel];
    NSString * pollutant = _data[@"pollutant"];
    if (pollutant.length == 1) {
        pollutant = @"各项指标良好！";
    }
}

- (UITextView *)createTextWithFrame:(CGRect)frame text:(NSString *)text color:(UIColor *)color font:(UIFont *)font
{
    UITextView * label = [[UITextView alloc] initWithFrame:frame];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setText:text];
    [label setTextColor:color];
    [label setFont:font];
    label.editable = NO;
    label.scrollEnabled = NO;
    return label;
}

- (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text color:(UIColor *)color font:(UIFont *)font
{
//    frame.size.height = _textHeight + 10;
    UILabel * label = [[UILabel alloc] initWithFrame:frame];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setText:text];
    [label setTextColor:color];
    [label setFont:font];
    label.numberOfLines = 0;
    return label;
}

#pragma mark - function
- (NSString *)getTextWithAQIValue:(NSUInteger)aqi
{
    NSArray * aqiDetail = @[
                            @"空气质量级别为一级，空气质量状况属于优。此时，空气质量令人满意，基本无空气污染，各类人群可正常活动。",
                            @"空气质量级别为二级，空气质量状况属于良。此时空气质量可接受，但某些污染物可能对极少数异常敏感人群健康有较弱影响，建议极少数异常敏感人群应减少户外活动。",
                            @"空气质量级别为三级，空气质量状况属于轻度污染。此时，易感人群症状有轻度加剧，健康人群出现刺激症状。建议儿童、老年人及心脏病、呼吸系统疾病患者应减少长时间、高强度的户外锻炼。",
                            @"空气质量级别为四级，空气质量状况属于中度污染。此时，进一步加剧易感人群症状，可能对健康人群心脏、呼吸系统有影响，建议疾病患者避免长时间、高强度的户外锻练，一般人群适量减少户外运动。",
                            @"空气质量级别为五级，空气质量状况属于重度污染。此时，心脏病和肺病患者症状显著加剧，运动耐受力降低，健康人群普遍出现症状，建议儿童、老年人和心脏病、肺病患者应停留在室内，停止户外运动，一般人群减少户外运动。",
                            @"空气质量级别为六级，空气质量状况属于严重污染。此时，健康人群运动耐受力降低，有明显强烈症状，提前出现某些疾病，建议儿童、老年人和病人应当留在室内，避免体力消耗，一般人群应避免户外活动。"
                            ];
    NSInteger count;
    if (aqi <= 50) {
        count = 0;
    }else if (aqi <= 100)
    {
        count = 1;
    }else if (aqi <= 150){
        count = 2;
    }else if (aqi <= 200){
        count = 3;
    }else if (aqi <= 300){
        count = 4;
    }else{
        count= 5;
    }
    NSString * string = aqiDetail[count];
//    _textHeight = [string boundingRectWithSize:CGSizeMake(ScreenWidth-10, 1000) options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]} context:nil].size.height;
    return string;
}

- (CGFloat)getFinalHeight
{
    return _textHeight;
}
















@end
